﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;

namespace TireService.Business.Portal.FinanceView
{
    public partial class InvoiceList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            // TEST parameter
            //Session["CustomerId"] = "10000";

            // Bind car data to gridview
            BindCustomerInvoice(Convert.ToInt32(Session["CustomerId"]));

            // Change gridview pagesize based on dropdownlist
            gvInvoice.SettingsPager.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
        }


        #region Private Functions

        /// <summary>
        /// Function that bind customer invoice to gridview
        /// </summary>
        /// <param name="CustomerId">Id on customer</param>
        private void BindCustomerInvoice(int CustomerId)
        {
            if (Session["InvoiceData"] == null)
            {
                EconomicInterface ecoInterface = new EconomicInterface();

                // Store invoices in session, to avoid API call to economic everytime
                Session["InvoiceData"] = ecoInterface.GetCustomerInvoiceList(CustomerId);
            }

            // Bind to gridview
            gvInvoice.DataSource = Session["InvoiceData"];
            gvInvoice.DataBind();
        }

        #endregion

        #region Control Functions

        /// <summary>
        /// Button function for export of gridview
        /// </summary>
        protected void btnExport_Click(object sender, EventArgs e)
        {
            if (ddlExport.Value == "0")
            {
                gvExport.WritePdfToResponse();
            }

            if (ddlExport.Value == "1")
            {
                gvExport.WriteXlsToResponse();
            }

            if (ddlExport.Value == "2")
            {
                gvExport.WriteCsvToResponse();
            }

            if (ddlExport.Value == "3")
            {
                gvExport.WriteRtfToResponse();
            }
        }

        #endregion


    }
}